﻿using Avalonia.Extensions.Controls;
using Bilibili.Common;
using Bilibili.Common.Log;
using Bilibili.Model.Bangumi;
using Bilibili.WebClient.Api;
using DynamicData;
using ReactiveUI;
using System;
using System.Collections.ObjectModel;
using System.Reactive;
using System.Threading.Tasks;

namespace Avalonia.BiliBili.Views.ViewModels
{
    public sealed class BangmiViewModel : ViewModelBase
    {
        private BangmiClient Client { get; }
        private int PageIndex { get; set; } = 1;
        public event CallbackNameObject ViewCallback;
        private bool showMine;
        public bool ShowMine
        {
            get => showMine;
            set => this.RaiseAndSetIfChanged(ref showMine, value);
        }
        public ReactiveCommand<string, Unit> OnButtonClick { get; }
        private ObservableCollection<MyBangumi> bangumis = [];
        public ObservableCollection<MyBangumi> Bangumis
        {
            get => bangumis;
            private set => this.RaiseAndSetIfChanged(ref bangumis, value);
        }
        private ObservableCollection<BangumiHome> jpBangumis = [];
        public ObservableCollection<BangumiHome> JpBangumis
        {
            get => jpBangumis;
            private set => this.RaiseAndSetIfChanged(ref jpBangumis, value);
        }
        private ObservableCollection<BangumiHome> cnBangumis = [];
        public ObservableCollection<BangumiHome> CnBangumis
        {
            get => cnBangumis;
            private set => this.RaiseAndSetIfChanged(ref cnBangumis, value);
        }
        public BangmiViewModel()
        {
            Client = new BangmiClient();
            OnButtonClick = ReactiveCommand.Create<string>(ButtonClick);
        }
        public async Task LoadData()
        {
            try
            {
                var result = await Client.GetMyFllow(PageIndex);
                if (result.IsSuccess())
                {
                    var m = result.Data;
                    if (m.Code == 0)
                    {
                        var mangumis = m.Result;
                        if (mangumis != null && mangumis.Count > 0)
                        {
                            ShowMine = true;
                            Bangumis.AddRange(mangumis);
                        }
                        else
                            PopupToast.Show("获取不到我的追番数据");
                    }
                    else
                        PopupToast.Show(m.Message);
                }
                else
                    PopupToast.Show(result.Message);
            }
            catch (Exception ex)
            {
                LogManager.Instance.LogError("ComicFragment LoadData", ex);
                PopupToast.Show("加载我的追番数据失败：" + ex.Message);
            }
            try
            {
                var result = await Client.GetRecommand();
                if (result.IsSuccess())
                {
                    var m = result.Data;
                    var jph = m.Recommend_Jp.Recommend;
                    if (jph != null && jph.Count > 0)
                        JpBangumis.AddRange(jph);
                    var cnh = m.Recommend_Cn.Recommend;
                    if (cnh != null && cnh.Count > 0)
                        CnBangumis.AddRange(cnh);
                }
                else
                    PopupToast.Show("加载番剧数据失败：" + result.Message);
            }
            catch (Exception ex)
            {
                LogManager.Instance.LogError("ComicFragment LoadData2", ex);
                PopupToast.Show("加载番剧数据失败：" + ex.Message);
            }
        }
        private void ButtonClick(string parameter)
        {
            ViewCallback?.Invoke(parameter, null);
        }
        public override void Dispose()
        {
            base.Dispose();
            bangumis?.Clear();
            jpBangumis?.Clear();
            cnBangumis?.Clear();
        }
    }
}